﻿using MatlabWork.Model;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using TestingPlatForm.MissionContrl.Dal;
using TestingPlatForm.MissionContrl.Entity;

namespace TestingPlatForm.MissionContrl.ViewModels
{
    public class SettingsViewModel : BindableBase
    {
        #region
        private string _ops_IP;

        public string ops_IP
        {
            get { return _ops_IP; }
            set { SetProperty(ref _ops_IP, value); }
        }


        private double _x_bs;

        public double x_bs
        {
            get { return _x_bs; }
            set { SetProperty(ref _x_bs, value); }
        }

        private double _y_bs;

        public double y_bs
        {
            get { return _y_bs; }
            set { SetProperty(ref _y_bs, value); }
        }

        private double _z_bs;

        public double z_bs
        {
            get { return _z_bs; }
            set { SetProperty(ref _z_bs, value); }
        }

        private double _fu;

        public double fu
        {
            get { return _fu; }
            set { SetProperty(ref _fu, value); }
        }


        private double _fu_up;

        public double fu_up
        {
            get { return _fu_up; }
            set { SetProperty(ref _fu_up, value); }
        }


        private double _fu_down;

        public double fu_down
        {
            get { return _fu_down; }
            set { SetProperty(ref _fu_down, value); }
        }


        private double _fu_up_ot;

        public double fu_up_ot
        {
            get { return _fu_up_ot; }
            set { SetProperty(ref _fu_up_ot, value); }
        }


        private double _fu_down_ot;

        public double fu_down_ot
        {
            get { return _fu_down_ot; }
            set { SetProperty(ref _fu_down_ot, value); }
        }

        private double _ff;

        public double ff
        {
            get { return _ff; }
            set { SetProperty(ref _ff, value); }
        }
        
        private double _ff_up;

        public double ff_up
        {
            get { return _ff_up; }
            set { SetProperty(ref _ff_up, value); }
        }
        
        private double _ff_down;

        public double ff_down
        {
            get { return _ff_down; }
            set { SetProperty(ref _ff_down, value); }
        }


        private double _ff_up_ot;

        public double ff_up_ot
        {
            get { return _ff_up_ot; }
            set { SetProperty(ref _ff_up_ot, value); }
        }

        private double _ff_down_ot;

        public double ff_down_ot
        {
            get { return _ff_down_ot; }
            set { SetProperty(ref _ff_down_ot, value); }
        }

        private double _ff_sway;

        public double ff_sway
        {
            get { return _ff_sway; }
            set { SetProperty(ref _ff_sway, value); }
        }

        private string _TextBlock;

        public string TextBlock
        {
            get { return _TextBlock; }
            set { SetProperty(ref _TextBlock, value); }
        }

        private string _bs;

        public string bs
        {
            get { return _bs; }
            set { SetProperty(ref _bs, value); }
        }

        private string _st;

        public string st
        {
            get { return _st; }
            set { SetProperty(ref _st, value); }
        }

        private double _x_st1;

        public double x_st1
        {
            get { return _x_st1; }
            set { SetProperty(ref _x_st1, value); }
        }

        private double _y_st1;

        public double y_st1
        {
            get { return _y_st1; }
            set { SetProperty(ref _y_st1, value); }
        }

        private double _z_st1;

        public double z_st1
        {
            get { return _z_st1; }
            set { SetProperty(ref _z_st1, value); }
        }

        private double _x_st2;

        public double x_st2
        {
            get { return _x_st2; }
            set { SetProperty(ref _x_st2, value); }
        }

        private double _y_st2;

        public double y_st2
        {
            get { return _y_st2; }
            set { SetProperty(ref _y_st2, value); }
        }

        private double _z_st2;

        public double z_st2
        {
            get { return _z_st2; }
            set { SetProperty(ref _z_st2, value); }
        }

#endregion
        public SettingsViewModel()
        {
            fu = GlobalEntity.configData.fu;
            fu_up = GlobalEntity.configData.fu_up;
            fu_down = GlobalEntity.configData.fu_down;
            fu_up_ot = GlobalEntity.configData.fu_up_ot;
            fu_down_ot = GlobalEntity.configData.fu_down_ot;
            ff = GlobalEntity.configData.ff;
            ff_up = GlobalEntity.configData.ff_up;
            ff_down = GlobalEntity.configData.ff_down;
            ff_up_ot = GlobalEntity.configData.ff_up_ot;
            ff_down_ot = GlobalEntity.configData.ff_down_ot;
            ff_sway = GlobalEntity.configData.ff_sway;
            st = GlobalEntity.configData.st;
            //bs = GlobalEntity.configData.bs;
            x_bs = GlobalEntity.configData.x_bs;
            y_bs = GlobalEntity.configData.y_bs;
            z_bs = GlobalEntity.configData.z_bs;
            x_st1 = GlobalEntity.configData.x_st1;
            y_st1 = GlobalEntity.configData .y_st1;
            z_st1 = GlobalEntity .configData .z_st1;
            x_st2 = GlobalEntity.configData .x_st2;
            y_st2 = GlobalEntity .configData .y_st2;
            z_st2 = GlobalEntity.configData.z_st2 ;
            TextBlock = GlobalEntity.configData.fileName;
            ops_IP = GlobalEntity.configData.OPS_IP;
        }

        public ICommand SelectCommand
        {
            get => new DelegateCommand(() =>
            {
                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = "All Files|*.*",

                };

                if (dialog.ShowDialog() == true && !string.IsNullOrWhiteSpace(dialog.FileName))
                {
                    // 获取用户选择的文件路径
                    TextBlock = dialog.FileName;
                }
            });
        }

        public ICommand SaveCommand
        {
            get => new DelegateCommand(() =>
            {
                GlobalEntity.configData.fu = fu;
                GlobalEntity.configData.fu_up = fu_up;
                GlobalEntity.configData.fu_down = fu_down;
                GlobalEntity.configData.fu_up_ot = fu_up_ot;
                GlobalEntity.configData.fu_down_ot = fu_down_ot;
                GlobalEntity.configData.ff = ff;
                GlobalEntity.configData.ff_up = ff_up;
                GlobalEntity.configData.ff_down = ff_down;
                GlobalEntity.configData.ff_up_ot = ff_up_ot;
                GlobalEntity.configData.ff_down_ot = ff_down_ot;
                GlobalEntity.configData.ff_sway = ff_sway;
                GlobalEntity.configData.st = st;
                //GlobalEntity.configData.bs = bs;
                GlobalEntity.configData.x_bs = x_bs;
                GlobalEntity.configData.y_bs = y_bs;
                GlobalEntity.configData.z_bs = z_bs;
                GlobalEntity.configData.x_st1 = x_st1;
                GlobalEntity.configData.y_st1 = y_st1;
                GlobalEntity.configData.z_st1 = z_st1;
                GlobalEntity.configData.x_st2 = x_st2;
                GlobalEntity.configData.y_st2 = y_st2;
                GlobalEntity.configData.z_st2 = z_st2;
                GlobalEntity.configData.fileName = TextBlock;

                string currentDirectory = Directory.GetCurrentDirectory();
                string logConfigFile = currentDirectory + "\\config\\BsSetting.config";
                if (File.Exists(logConfigFile))
                {
                    string[] readText = File.ReadAllLines(logConfigFile);
                    for (int nIndex = 0; nIndex < readText.Length; nIndex++)
                    {
                        string line = readText[nIndex];
                        if (line.Contains("fu="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("fu={0}", fu);
                        }
                        if (line.Contains("fu_up="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("fu_up={0}", fu_up);
                        }
                        if (line.Contains("fu_down="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("fu_down={0}", fu_down);
                        }
                        if (line.Contains("fu_up_ot="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("fu_up_ot={0}", fu_up_ot);
                        }
                        if (line.Contains("fu_down_ot="))
                        {
                            // 用户链路KU
                            readText[nIndex] = string.Format("fu_down_ot={0}", fu_down_ot);
                        }
                        if (line.Contains("ff="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff={0}", ff);
                        }
                        if (line.Contains("ff_up="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff_up={0}", ff_up);
                        }
                        if (line.Contains("ff_down="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff_down={0}", ff_down);
                        }
                        if (line.Contains("ff_up_ot="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff_up_ot={0}", ff_up_ot);
                        }
                        if (line.Contains("ff_down_ot="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff_down_ot={0}", ff_down_ot);
                        }
                        if (line.Contains("ff_sway="))
                        {
                            // 馈电链路QV
                            readText[nIndex] = string.Format("ff_sway={0}", ff_sway);
                        }
                        if (line.Contains("st="))
                        {
                            // 终端X
                            readText[nIndex] = string.Format("st={0}", st);
                        }
                        if (line.Contains("x_bs="))
                        {
                            // 基站X
                            readText[nIndex] = string.Format("x_bs={0}", x_bs);
                        }
                        if (line.Contains("y_bs="))
                        {
                            // 基站X
                            readText[nIndex] = string.Format("y_bs={0}", y_bs);
                        }
                        if (line.Contains("z_bs="))
                        {
                            // 基站X
                            readText[nIndex] = string.Format("z_bs={0}", z_bs);
                        }
                        if (line.Contains("fileName="))
                        {
                            // 基站Y
                            readText[nIndex] = string.Format("fileName={0}", TextBlock);
                        }
                        if (line.Contains("x_st1="))
                        {
                            // 终端X2
                            readText[nIndex] = string.Format("x_st1={0}", x_st1);
                        }
                        if (line.Contains("y_st1="))
                        {
                            // 终端Y2a
                            readText[nIndex] = string.Format("y_st1={0}", y_st1);
                        }

                        if (line.Contains("z_st1="))
                        {
                            readText[nIndex] = string.Format("z_st1={0}", z_st1);
                        }

                        if (line.Contains("x_st2="))
                        {
                            // 终端X2
                            readText[nIndex] = string.Format("x_st2={0}", x_st2);
                        }
                        if (line.Contains("y_st2="))
                        {
                            // 终端Y2a
                            readText[nIndex] = string.Format("y_st2={0}", y_st2);
                        }

                        if (line.Contains("z_st2="))
                        {
                            readText[nIndex] = string.Format("z_st2={0}", z_st2);
                        }

                        if (line.Contains("OPS_IP="))
                        {
                            readText[nIndex] = string.Format("OPS_IP={0}", ops_IP);
                        }
                    }

                    // 重写入配置文件
                    File.WriteAllLines(logConfigFile, readText);

                    MessageBox.Show("配置保存成功！");
                }
                InitialVal.RefreshValue();
            });
        }
    }
}
